package com.yoyo.itravel.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.yoyo.itravel.constants.ShortMsgConstant;

/**
 * @author HZX
 * @Description 短信工具类
 * @date 2020/4/26 0:11
 */
public class ShortMsgUtils {

    private static String code;

    private ShortMsgUtils() {
    }

    /**
     * @Description 发送短信
     *
     * @param phone
     * @return java.lang.String
     */
    public static String sendShortMsg(String phone) {
        // 判断手机号是否
        // 构建DefaultProfile对象
        DefaultProfile profile = DefaultProfile.getProfile(ShortMsgConstant.REGION_ID, ShortMsgConstant.ACCESS_KEYID, ShortMsgConstant.ACCESS_KEYSECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        // 组装请求对象
        CommonRequest request = new CommonRequest();
        // 使用post提交
        request.setMethod(MethodType.POST);
        // 短信API产品域名（接口地址固定，无需修改）
        request.setDomain(ShortMsgConstant.DOMAIN);
        request.setVersion(ShortMsgConstant.VERSION);
        request.setAction(ShortMsgConstant.ACTION);
        request.putQueryParameter("RegionId", ShortMsgConstant.REGION_ID);
        // 待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码
        request.putQueryParameter("PhoneNumbers", phone);
        // 短信签名-可在短信控制台中找到
        request.putQueryParameter("SignName", ShortMsgConstant.SIGN_NAME);
        // 短信模板码-可在短信控制台中找到
        request.putQueryParameter("TemplateCode", ShortMsgConstant.TEMPLATE_CODE);
        // 生成随机验证码
        String code = StringUtils.getRandomVerifyCode();
        // 可选:模板中的变量使用JSON替换
        request.putQueryParameter("TemplateParam", "{'code' : '" + code + "'}");
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
            // System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return code;
    }

//    public static void main(String[] args) {
//        sendShortMsg("13710277866");
//    }

}
